package com.git.hui.yinshua.biz.rest.admin.banner;

import com.git.hui.yinshua.api.model.ExceptionUtil;
import com.git.hui.yinshua.api.model.ListRsp;
import com.git.hui.yinshua.api.model.ResVo;
import com.git.hui.yinshua.api.model.Status;
import com.git.hui.yinshua.biz.banner.service.BannerService;
import com.git.hui.yinshua.biz.rest.models.banner.req.BannerReq;
import com.git.hui.yinshua.biz.rest.models.banner.res.BannerDto;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * 运营banner
 *
 * @author YiHui
 * @date 2024/6/17
 */
@RestController
@RequestMapping(path = "admin/banner")
public class AdminBannerController {
    @Autowired
    private BannerService bannerService;

    @PostMapping(path = "add")
    public ResVo<BannerDto> add(@RequestBody BannerReq req) {
        if (req.getTab() == null) {
            throw ExceptionUtil.of(Status.StatusEnum.ILLEGAL_ARGUMENTS_MIXED, "请指定适用范围!");
        }
        if (StringUtils.isBlank(req.getImg())) {
            throw ExceptionUtil.of(Status.StatusEnum.ILLEGAL_ARGUMENTS_MIXED, "图片不能为空");
        }

        return ResVo.ok(bannerService.add(req));
    }

    @PostMapping(path = "update")
    public ResVo<Boolean> update(@RequestBody BannerReq req) {
        if (req.getBannerId() == null) {
            throw ExceptionUtil.of(Status.StatusEnum.ILLEGAL_ARGUMENTS_MIXED, "请指定需要更新的banner");
        }

        return ResVo.ok(bannerService.update(req));
    }

    @GetMapping(path = "online")
    public ResVo<Boolean> online(@RequestParam(name = "bannerId") Integer bannerId, @RequestParam(name = "online") Boolean online) {
        return ResVo.ok(bannerService.online(bannerId, online));
    }

    @GetMapping(path = "delete")
    public ResVo<Boolean> delete(@RequestParam(name = "bannerId") Integer bannerId) {
        return ResVo.ok(bannerService.delete(bannerId));
    }

    @RequestMapping(path = "list")
    public ResVo<ListRsp<BannerDto>> list(BannerReq req) {
        req.autoInitReq();
        ListRsp<BannerDto> list = bannerService.list(req);
        return ResVo.ok(list);
    }
}
